package com.example.mpandroidchart1107;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

public class Main3Activity extends AppCompatActivity {
    private Button lin,bar,rad,pao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        setTitle("饼状图");
        pao=findViewById(R.id.pao);
        rad=findViewById(R.id.rad);
        lin=findViewById(R.id.lin);
        bar=findViewById(R.id.bar);
        PieChart pieChart=findViewById(R.id.piechart);//实例化


        List<PieEntry> strings = new ArrayList<>();//调用饼图方法

        strings.add(new PieEntry(60f,"拜登"));
        strings.add(new PieEntry(30f,"特朗普"));
        strings.add(new PieEntry(60f,"奥巴马"));
        strings.add(new PieEntry(99f,"马保国"));

        PieDataSet dataSet = new PieDataSet(strings,"支持率%");

        ArrayList<Integer> colors = new ArrayList<Integer>();

        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.colorPrimary));
        colors.add(getResources().getColor(R.color.colorPrimaryDark));
        colors.add(getResources().getColor(R.color.mabaoguo));

        dataSet.setColors(colors);


        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);//是否在表上显示数据 ValueFormatter（格式化数据值）
        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextSize(12f);//数据显示的字体大小

        //把数字变成百分比
        pieData.setDrawValues(true);

        Description description = new Description();//更改右下角Description 显示内容
        description.setText("竞选比率饼状图");
        description.setTextSize(12f);//字体大小
        pieChart.setDescription(description);

        /*  消除半径空隙*/
        pieChart.setHoleRadius(0f);//设成半径为零
        pieChart.setTransparentCircleRadius(0f); //再设置半透明的半径为0

        pieChart.setData(pieData);//调用方法
        pieChart.invalidate();


        setListeren();//设置监听器   ，写在方法后面，不然会闪退
    }
    private void setListeren() {  //跳转

        /*泡泡图*/
        pao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3=new Intent(Main3Activity.this,Main5Activity.class);
                startActivity(intent3);
            }
        });
        /*雷达图*/
        rad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2=new Intent(Main3Activity.this,Main4Activity.class);
                startActivity(intent2);
            }
        });
        /*线型图*/
        lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(Main3Activity.this,MainActivity.class);
                startActivity(intent1);
            }
        });
        /*柱形图*/
        bar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Main3Activity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

    }

}